package cn.autumnorange.app.user.consumer.authorization.service;

import cn.autumnorange.app.common.consumer.gts.GtsConsumerService;
import cn.autumnorange.app.common.dto.ResultDto;
import cn.autumnorange.app.common.rpc.RPCWrapper;
import cn.autumnorange.app.user.api.AppUserService;
import cn.autumnorange.app.user.api.MoneyService;
import cn.autumnorange.app.user.api.SysRoleService;
import cn.autumnorange.app.user.api.dto.GramtedAuthorityImpl;
import cn.autumnorange.app.user.api.dto.UserDetailDto;
import cn.autumnorange.app.user.consumer.authorization.system.userDetails.UserDetailsImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
// @Transactional(rollbackFor = Exception.class, readOnly = true, propagation =
// Propagation.REQUIRED)
@Service(value = "userDetailsServiceImpl")
public class UserDetailsServiceImpl implements UserDetailsService {
  @Autowired private GtsConsumerService gtsConsumerServiceImpl;

  @Autowired private AppUserService appUserService;
  @Autowired private SysRoleService sysRoleService;
  @Autowired private MoneyService moneyService;

  @Value("${server.port}")
  private int port;
  //    @Autowired
  //    private GtsService gtsService;
  // 使用gts事务无法进来
  //  @TxcTransaction(timeout = 4000)
  @Override
  public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
    //        gtsService.setXidHeader();
    //        ResultDto<MoneyDto> moneyDtoResultDto = moneyService.getmoney();
    //        if (moneyDtoResultDto.getCode() != 200) {
    //            throw new RuntimeException(moneyDtoResultDto.getMessage());
    //        }
    log.info(
        "authorization 端口:"
            + port
            + "rpcId:"
            + RPCWrapper.getHttpServletRequestRPCWrapper().getHeader("rpcid"));

    ResponseEntity<ResultDto<UserDetailDto>> resultDto = null;
    UserDetailDto userDetailDto = null;

    //    try {
    //      gtsConsumerServiceImpl.setXidHeader();
    resultDto = appUserService.loadUserByUsername("admin");
    if (resultDto == null || resultDto.getStatusCodeValue() != HttpStatus.OK.value()) {
      throw new RuntimeException(resultDto.getBody().getMessage());
    }

    userDetailDto = resultDto.getBody().getData();
    List<GramtedAuthorityImpl> gramtedAuthorityList = null;
    ResponseEntity<ResultDto<List<GramtedAuthorityImpl>>> gramtedAuthorities =
        sysRoleService.getRolesByUserId(userDetailDto.getId());
    if (gramtedAuthorities.getStatusCodeValue() != HttpStatus.OK.value()) {
      throw new UsernameNotFoundException(resultDto.getBody().getMessage());
    }
    gramtedAuthorityList = gramtedAuthorities.getBody().getData();

    UserDetailsImpl userDetails =
        new UserDetailsImpl(
            userDetailDto.getId(),
            userDetailDto.getUsername(),
            userDetailDto.getPassword(),
            gramtedAuthorityList,
            userDetailDto.getAccountNonExpired(),
            userDetailDto.getAccountNonLocked(),
            userDetailDto.getCredentialsNonExpired(),
            userDetailDto.getEnabled());
    return userDetails;
    //    } catch (Exception e) {
    //      throw e;
    //    } finally {
    //      gtsConsumerServiceImpl.removeXidHeader();
    //    }
  }
}
